<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Reserves;
use App\Models\IntervieweeInformation;
use App\Models\Visitor;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use function Symfony\Component\String\u;
use App\Models\Reserves as ReservesModel;
class ReservesController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Reserves(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('visitors_id');
            $grid->column('interviewee_information_id');
            $grid->column('reason');
            $grid->column('device_groups');
            $grid->column('source');
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Reserves(), function (Show $show) {
            $show->field('id');
            $show->field('visitors_id');
            $show->field('interviewee_information_id');
            $show->field('reason');
            $show->field('device_groups');
            $show->field('source');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Reserves(['visitor']), function (Form $form) {
            $form->display('id');

            $form->multipleSelect('visitor')
                ->options(Visitor::all()->pluck('visitorName', 'id'))
                ->customFormat(function ($v) {
                    if (!$v) {
                        return [];
                    }

                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                })->required();

            $form->select('interviewee_information_id')->options(function () {
                return IntervieweeInformation::all()->pluck('name', 'id');
            })->saving(function ($value) {
                return (int)$value;
            })->required();

            $form->select('reason')->options(ReservesModel::$reason)->required();
            $form->multipleSelect('device_groups')->options(ReservesModel::$deviceGroups)->default(12)->required();
            $form->select('source')->options(ReservesModel::$source)->default(3)->required();

            $form->time('start_time')->default('00:00:00')->required();
            $form->time('end_time')->default('23:59:59')->required();
            $form->switch('status')->default(1)->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
